<?php
namespace app\api\service\ceping;

class XinlizhengzhuangService {
    // 基于PHQ-9、GAD-7等标准化量表设计
	//（0=从未，3=几乎每天）
    private $assessmentConfig = [
        'depression' => [
            'name' => '抑郁倾向',
            'questions' => [
                ['id' => 1, 'text' => '感到情绪低落、沮丧', 'type' => 'frequency'],
                ['id' => 2, 'text' => '对事物失去兴趣', 'type' => 'frequency'],
                ['id' => 3, 'text' => '睡眠问题（难以入睡/嗜睡）', 'type' => 'frequency'],
                ['id' => 4, 'text' => '感到疲倦或没有精力', 'type' => 'frequency'],
                ['id' => 5, 'text' => '食欲异常（暴食/食欲减退）', 'type' => 'frequency']
            ],
            'thresholds' => [5 => '轻度', 10 => '中度', 15 => '重度']
        ],
        'anxiety' => [
            'name' => '焦虑倾向',
            'questions' => [
                ['id' => 6, 'text' => '感到紧张、焦虑', 'type' => 'frequency'],
                ['id' => 7, 'text' => '难以控制的担忧', 'type' => 'frequency'],
                ['id' => 8, 'text' => '坐立不安', 'type' => 'frequency'],
                ['id' => 9, 'text' => '容易烦恼或急躁', 'type' => 'frequency'],
                ['id' => 10, 'text' => '害怕会有可怕的事情发生', 'type' => 'frequency']
            ],
            'thresholds' => [5 => '轻度', 10 => '中度', 15 => '重度']
        ],
        'stress' => [
            'name' => '压力水平',
            'questions' => [
                ['id' => 11, 'text' => '感到难以应对日常事务', 'type' => 'frequency'],
                ['id' => 12, 'text' => '感到紧张或压力大', 'type' => 'frequency'],
                ['id' => 13, 'text' => '容易生气或烦躁', 'type' => 'frequency'],
                ['id' => 14, 'text' => '注意力难以集中', 'type' => 'frequency'],
                ['id' => 15, 'text' => '出现身体症状（头痛/胃痛等）', 'type' => 'frequency']
            ],
            'thresholds' => [5 => '轻度', 10 => '中度', 15 => '重度']
        ]
    ];

    // 生成标准化问卷
    public function generateTest() {
        $questionnaire = [];
        foreach ($this->assessmentConfig as $category => $config) {
            $questionnaire = array_merge($questionnaire, $config['questions']);
        }
        shuffle($questionnaire);
        return $questionnaire;
    }

    // 计算评估结果
    public function calculateScore($answers) {
        $results = [];
        
        foreach ($this->assessmentConfig as $category => $config) {
            $total = 0;
            foreach ($config['questions'] as $q) {
                $total += $answers[$q['id']] ?? 0;
            }
            $level = $this->determineLevel($total, $config['thresholds']);
            $results[$category] = [
                'name' => $config['name'],
                'score' => $total,
                'level' => $level,
                'max' => count($config['questions']) * 3 // 每题0-3分
            ];
        }
		
		$report = $this->generateReport($results);
        return $report;
    }

    // 判断严重程度
    private function determineLevel($score, $thresholds) {
        krsort($thresholds);
        foreach ($thresholds as $min => $level) {
            if ($score >= $min) return $level;
        }
        return '正常范围';
    }

    // 生成评估报告
    public function generateReport($results) {

        $report = [];
        foreach ($results as $category => $data) {
            $percentage = round(($data['score'] / $data['max']) * 100, 1);
            $report[] = [
                'category' => $data['name'],
                //'score' => $data['score'],
                //'max' => $data['max'],
                'level' => $data['level'],
                //'percentage' => $percentage,
                'advice' => $this->generateAdvice($data['name'], $data['level'])
            ];
        }
        return $report;
    }

    // 生成专业建议
    private function generateAdvice($category, $level) {
        $adviceMap = [
            '抑郁倾向' => [
                '轻度' => '建议进行正念练习，保持社交活动',
                '中度' => '推荐心理咨询，可尝试认知行为疗法',
                '重度' => '请立即联系专业心理医生'
            ],
            '焦虑倾向' => [
                '轻度' => '尝试深呼吸放松训练',
                '中度' => '推荐使用焦虑管理APP（如Calm）',
                '重度' => '建议进行专业心理评估'
            ],
            '压力水平' => [
                '轻度' => '适当进行运动减压',
                '中度' => '推荐时间管理培训',
                '重度' => '需要系统性压力管理干预'
            ]
        ];
        return $adviceMap[$category][$level] ?? '保持健康生活习惯';
    }
}

?>